| Conditions | 1 |
| Paths | 1 |
| Total Lines | 194 |
| Code Lines | 126 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | // load cookie lib |
||
| 9 | describe('cookie', function() { |
||
| 10 | beforeAll(function() { |
||
| 11 | cookiejs.global.cookie = ''; |
||
| 12 | }); |
||
| 13 | |||
| 14 | describe('set function default values', function() { |
||
| 15 | it('should be: "defaults=; path=/"', function() { |
||
| 16 | cookiejs.set('defaults'); |
||
| 17 | expect(cookiejs.global.cookie).toEqual('defaults=; path=/'); |
||
| 18 | }); |
||
| 19 | }); |
||
| 20 | |||
| 21 | describe('argument sCookieName', function() { |
||
| 22 | it('should be defined', function() { |
||
| 23 | expect(cookiejs.set).toThrowError( |
||
| 24 | TypeError, |
||
| 25 | 'sCookieName is not of type string' |
||
| 26 | ); |
||
| 27 | }); |
||
| 28 | |||
| 29 | it('should be a non falsy value', function() { |
||
| 30 | expect(cookiejs.set.bind(null, '')).toThrowError(TypeError); |
||
| 31 | }); |
||
| 32 | |||
| 33 | it('should be a string', function() { |
||
| 34 | expect(cookiejs.set.bind(null, 123)).toThrowError(TypeError); |
||
| 35 | }); |
||
| 36 | }); |
||
| 37 | |||
| 38 | describe('argument sValue', function() { |
||
| 39 | it('can be undefined', function() { |
||
| 40 | cookiejs.set('test'); |
||
| 41 | expect(cookiejs.global.cookie).toEqual('test=; path=/'); |
||
| 42 | }); |
||
| 43 | |||
| 44 | it('if given should be of type string', function() { |
||
| 45 | expect(cookiejs.set.bind(null, 'test', function() {})).toThrowError( |
||
| 46 | TypeError |
||
| 47 | ); |
||
| 48 | expect(cookiejs.set.bind(null, 'test', 1)).toThrowError(TypeError); |
||
| 49 | expect(cookiejs.set.bind(null, 'test', [])).toThrowError(TypeError); |
||
| 50 | expect(cookiejs.set.bind(null, 'test', {})).toThrowError(TypeError); |
||
| 51 | }); |
||
| 52 | |||
| 53 | it('should result in empty string for falsy values', function() { |
||
| 54 | cookiejs.set('test', ''); |
||
| 55 | expect(cookiejs.global.cookie).toEqual('test=; path=/'); |
||
| 56 | cookiejs.set('test', false); |
||
| 57 | expect(cookiejs.global.cookie).toEqual('test=; path=/'); |
||
| 58 | cookiejs.set('test', 0); |
||
| 59 | expect(cookiejs.global.cookie).toEqual('test=; path=/'); |
||
| 60 | cookiejs.set('test', null); |
||
| 61 | expect(cookiejs.global.cookie).toEqual('test=; path=/'); |
||
| 62 | cookiejs.set('test', undefined); |
||
| 63 | expect(cookiejs.global.cookie).toEqual('test=; path=/'); |
||
| 64 | }); |
||
| 65 | }); |
||
| 66 | |||
| 67 | describe('argument oAttribute', function() { |
||
| 68 | it('should be applied correctly', function() { |
||
| 69 | cookiejs.set.bind(null, 'test', 'test', { |
||
| 70 | domain: '.somedomain.com', |
||
| 71 | path: '/somepath', |
||
| 72 | 'max-age': '60', |
||
| 73 | secure: true, |
||
| 74 | expires: 'atsometime' |
||
| 75 | })(); |
||
| 76 | expect(cookiejs.global.cookie).toEqual( |
||
| 77 | 'test=test;domain=.somedomain.com;path=/somepath;max-age=60;secure;expires=atsometime' |
||
| 78 | ); |
||
| 79 | }); |
||
| 80 | |||
| 81 | it('should be of type object', function() { |
||
| 82 | expect(cookiejs.set.bind(null, 'test', 'test', [])).toThrowError( |
||
| 83 | TypeError |
||
| 84 | ); |
||
| 85 | expect(cookiejs.set.bind(null, 'test', 'test', false)).toThrowError( |
||
| 86 | TypeError |
||
| 87 | ); |
||
| 88 | expect(cookiejs.set.bind(null, 'test', 'test', 'options')).toThrowError( |
||
| 89 | TypeError |
||
| 90 | ); |
||
| 91 | expect(cookiejs.set.bind(null, 'test', 'test', 0)).toThrowError( |
||
| 92 | TypeError |
||
| 93 | ); |
||
| 94 | }); |
||
| 95 | |||
| 96 | it('property domain should be of type string', function() { |
||
| 97 | expect( |
||
| 98 | cookiejs.set.bind(null, 'test', 'test', { |
||
| 99 | domain: 1 |
||
| 100 | }) |
||
| 101 | ).toThrowError(TypeError); |
||
| 102 | expect( |
||
| 103 | cookiejs.set.bind(null, 'test', 'test', { |
||
| 104 | domain: [] |
||
| 105 | }) |
||
| 106 | ).toThrowError(TypeError); |
||
| 107 | }); |
||
| 108 | |||
| 109 | it('property path should be of type string', function() { |
||
| 110 | expect( |
||
| 111 | cookiejs.set.bind(null, 'test', 'test', { |
||
| 112 | path: 1 |
||
| 113 | }) |
||
| 114 | ).toThrowError(TypeError); |
||
| 115 | expect( |
||
| 116 | cookiejs.set.bind(null, 'test', 'test', { |
||
| 117 | path: {} |
||
| 118 | }) |
||
| 119 | ).toThrowError(TypeError); |
||
| 120 | }); |
||
| 121 | |||
| 122 | it('property max-age should be of type string', function() { |
||
| 123 | expect( |
||
| 124 | cookiejs.set.bind(null, 'test', 'test', { |
||
| 125 | 'max-age': function() {} |
||
| 126 | }) |
||
| 127 | ).toThrowError(TypeError); |
||
| 128 | expect( |
||
| 129 | cookiejs.set.bind(null, 'test', 'test', { |
||
| 130 | 'max-age': 60 |
||
| 131 | }) |
||
| 132 | ).toThrowError(TypeError); |
||
| 133 | }); |
||
| 134 | |||
| 135 | it('property expires should be of type string', function() { |
||
| 136 | expect( |
||
| 137 | cookiejs.set.bind(null, 'test', 'test', { |
||
| 138 | expires: 123 |
||
| 139 | }) |
||
| 140 | ).toThrowError(TypeError); |
||
| 141 | }); |
||
| 142 | |||
| 143 | it('property secure should be of type boolean', function() { |
||
| 144 | expect( |
||
| 145 | cookiejs.set.bind(null, 'test', 'test', { |
||
| 146 | secure: 0 |
||
| 147 | }) |
||
| 148 | ).toThrowError(TypeError); |
||
| 149 | expect( |
||
| 150 | cookiejs.set.bind(null, 'test', 'test', { |
||
| 151 | secure: 1 |
||
| 152 | }) |
||
| 153 | ).toThrowError(TypeError); |
||
| 154 | expect( |
||
| 155 | cookiejs.set.bind(null, 'test', 'test', { |
||
| 156 | secure: 'false' |
||
| 157 | }) |
||
| 158 | ).toThrowError(TypeError); |
||
| 159 | }); |
||
| 160 | }); |
||
| 161 | |||
| 162 | describe('cookie remove', function() { |
||
| 163 | it('is performed correctly', function() { |
||
| 164 | cookiejs.remove('test', { |
||
| 165 | domain: '.somedomain.com', |
||
| 166 | path: '/somepath', |
||
| 167 | 'max-age': '60' |
||
| 168 | }); |
||
| 169 | expect(cookiejs.global.cookie).toEqual( |
||
| 170 | 'test=;domain=.somedomain.com;path=/somepath;max-age=60;expires=Thu, 01 Jan 1970 00:00:01 GMT' |
||
| 171 | ); |
||
| 172 | cookiejs.remove('test'); |
||
| 173 | expect(cookiejs.global.cookie).toEqual( |
||
| 174 | 'test=;expires=Thu, 01 Jan 1970 00:00:01 GMT' |
||
| 175 | ); |
||
| 176 | }); |
||
| 177 | |||
| 178 | it('throws TypeError bco sCookieName', function() { |
||
| 179 | expect(cookiejs.remove.bind(null)).toThrowError(TypeError); |
||
| 180 | }); |
||
| 181 | |||
| 182 | it('throws TypeError bco oAttributes', function() { |
||
| 183 | expect(cookiejs.remove.bind(null, 'test', 123)).toThrowError(TypeError); |
||
| 184 | }); |
||
| 185 | }); |
||
| 186 | |||
| 187 | describe('get cookie value', function() { |
||
| 188 | it('is performed correctly', function() { |
||
| 189 | expect( |
||
| 190 | (function() { |
||
| 191 | cookiejs.global.cookie = |
||
| 192 | '_ga=GA1.2.275361089.1527101378; _gcl_au=1.1.697370153.1537362740; _gid=GA1.2.476548619.1537816787'; |
||
| 193 | return cookiejs.get('_gcl_au'); |
||
| 194 | })() |
||
| 195 | ).toEqual('1.1.697370153.1537362740'); |
||
| 196 | }); |
||
| 197 | |||
| 198 | it('throws TypeError', function() { |
||
| 199 | expect(cookiejs.get.bind(null)).toThrowError(TypeError); |
||
| 200 | }); |
||
| 201 | }); |
||
| 202 | }); |
||
| 203 |